package com.vftour.stu.base.thread;

import java.util.concurrent.*;

/**
 * Thread
 *
 * @version 1.0
 * @author: 东东 | d@tke.store
 * @date :  2022/2/22 9:57 下午
 */
public class ThreadDemo {
    private static ExecutorService service = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws Exception{
        System.out.println("main start ......");


        /**
         * 1. new ThreadTest().start();
         *
         *         2. new Thread(() -> {
         *             System.out.println("当前线程：" + Thread.currentThread().getId());
         *             int i = 10/2;
         *             System.out.println("运行结果：" + i);
         *         }).start();
         *
         *   3.
         */
//        FutureTask<Integer> futureTask = new FutureTask<Integer>(new CallableTest());
//        new Thread(futureTask).start();
//
//        //阻塞等待
//        Integer i = futureTask.get();
//
//
//
////        service.submit() 可以返回值
//        service.execute(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        });
//
//        ThreadPoolExecutor executor = new ThreadPoolExecutor(
//                5,
//                100,
//                10,
//                TimeUnit.SECONDS,
//                new LinkedBlockingDeque(1000),
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy()
//                );


//        CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10/2;
//            System.out.println("运行结果：" + i);
//        },service);

        CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果：" + i);
            return i;
        },service).whenComplete((res,exc) -> {
            System.out.println("运行结果：" + res);
        }).exceptionally(throwable -> {
            return 10;
        });

        System.out.println("main end ......");
    }

    public static class ThreadTest extends Thread {

        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果：" + i);
        }
    }

    public static class CallableTest implements Callable {
        @Override
        public Object call() throws Exception {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("运行结果：" + i);
            return i;
        }
    }
}
